<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="bookcaseid" label="分类ID" width="100" align="center"></el-table-column>
      <el-table-column prop="categoryName" label="分类名称" width="180"></el-table-column>
      <el-table-column prop="description" label="分类描述"></el-table-column>
      <el-table-column prop="bookCount" label="图书数量" width="120" align="center"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button 
              @click="edit(scope.row)" 
              type="primary" 
              icon="el-icon-edit" 
              size="small"
              circle
              style="margin-right: 10px">
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button 
              @click="deleteCategory(scope.row)" 
              type="danger" 
              icon="el-icon-delete" 
              size="small"
              circle>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination 
      background 
      layout="prev, pager, next, sizes, jumper, ->, total"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize" 
      :total="total" 
      :current-page="currentPage"
      @current-change="page"
      @size-change="handleSizeChange"
      style="margin-top: 20px;">
      <template #default="{pager}">
        <span class="el-pagination__total">
          共 {{ total }} 条记录 第{{ currentPage }}/{{ Math.ceil(total/pageSize) }}页
        </span>
      </template>
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    deleteCategory(row) {
      const _this = this
      axios.delete('http://localhost:8181/bookCategory/deleteById/' + row.bookcaseid).then(function (resp) {
        _this.$alert('《' + row.categoryName + '》删除成功！', '消息', {
          confirmButtonText: '确定',
          callback: action => {
            _this.fetchData(_this.currentPage)
          }
        })
      })
    },
    edit(row) {
      this.$router.push({
        path: '/admin/category/update',
        query: {
          bookcaseid: row.bookcaseid
        }
      })
    },
    page(currentPage) {
      this.currentPage = currentPage
      this.fetchData(currentPage)
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.currentPage = 1
      this.fetchData(1)
    },
    fetchData(page) {
      const _this = this
      const pageIndex = page - 1
      
      _this.tableData = []
      _this.loading = true
      
      axios.get('http://localhost:8181/bookCategory/findAll/' + pageIndex + '/5').then(function (resp) {
            _this.tableData = resp.data.data.map(item => ({
              ...item.category,
              bookCount: item.bookCount
            }))
            _this.total = resp.data.total
            _this.currentPage = page
      }).catch(error => {
        console.error('获取数据失败:', error)
        _this.$message.error('加载数据失败')
      }).finally(() => {
        _this.loading = false
      })
    }
  },

  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: []
    }
  },

  created() {
    this.fetchData(1)
  }
}
</script>
